home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 08 - 1992 / 08.03 Jul 92 / Equation Compiler / CustomFn.c next >
Encoding:
C/C++ Source or Header  |  1991-05-17  |  1.3 KB  |  75 lines  |  [TEXT/KAHL]

  1. /* Contains the custom unary functions */
  2.  
  3. #include    <SANE.h>
  4. #include    "EqnCompiler.h"
  5.  
  6. /* Number of custom functions defined */
  7. #define    NUM_CF    2
  8.  
  9. /* Declaration of custom functions */
  10. extended    saneasin(extended);
  11. extended    saneacos(extended);
  12.  
  13. /* Array of custom function pointers */
  14. ProcPtr    CFPtr[] = {
  15.     (void *)    saneasin,
  16.     (void *)    saneacos
  17. };
  18.  
  19. /* Array of pointers to custom keywords */
  20. char    *CFKeyword[] = {
  21.     "\pasin",
  22.     "\pacos"
  23. };
  24.  
  25. /* The custom functions */
  26. static int    _C[] = {0x3FDE, 0x8000, 0x0000, 0x0000, 0x0000};
  27. #define    C    (* (extended *) _C)
  28. extended    saneasin(extended x)
  29. {
  30.     environment    env;
  31.     extended        y;
  32.     
  33.     procentry(&env);
  34.     y = fabs(x);
  35.     if (y > C) {
  36.         if (y > 0.5) {
  37.             y = 1 - y;
  38.             y = 2*y - y*y;
  39.         } else y = 1 - y*y;
  40.         y = atan(x/sqrt(y));
  41.         /* may give divide by zero exception */
  42.         setexception(DIVBYZERO, false);
  43.     } else y = x;
  44.     procexit(env);
  45.     return y;
  46. }
  47.  
  48. extended    saneacos(extended x)
  49. {
  50.     environment    env;
  51.     extended        y;
  52.     
  53.     procentry(&env);
  54.     y = 2*atan(sqrt((1-x)/(1+x)));
  55.     /* may give divide by zero exception */
  56.     setexception(DIVBYZERO, false);
  57.     procexit(env);
  58.     return y;
  59. }
  60.  
  61. /*    look up custom keywords */
  62. int    LookUpCF(char *str, int    *result)
  63. {
  64.     int    i;
  65.     
  66.     for (i=0; i<NUM_CF; i++) {
  67.         if (EqualString(str, CFKeyword[i], 0, 1)) {
  68.             *result = UN_FUNC + CUSTOM + i;
  69.             return 0;
  70.         }
  71.     }
  72.     return badTokenErr;
  73. }
  74.  
  75.